package com.netease.nim.demo.session.viewholder;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.netease.nim.demo.common.ui.imageview.MsgThumbImageView;
import com.netease.nim.demo.common.util.media.ImageUtil;
import com.netease.nim.demo.common.util.string.StringUtil;
import com.netease.nim.demo.session.model.MsgListItem;
import com.netease.nimlib.sdk.msg.attachment.VideoAttachment;
import com.netease.nimlib.sdk.msg.constant.AttachStatusEnum;
import com.shwread.android.qysw10000136.R;


public class MsgViewHolderVideoRight extends MsgViewHolder {
	public MsgThumbImageView thumbnailImageView;
	protected View maskImageView;
	
	private TextView progressTextView;

	protected int getResId() {
		return R.layout.video_message_view_right_item;
	}

	@Override
	protected int getViewHolderDirection() {
		return VIEW_HOLDER_RIGHT;
	}

	public void inflate() {
		super.inflate();

		thumbnailImageView = (MsgThumbImageView)view.findViewById(R.id.imageViewThumbnail);
		longClickView = thumbnailImageView;
		maskImageView = view.findViewById(R.id.imageViewMask);
		
		progressLayout = view.findViewById(R.id.progressLayout);
		progressTextView = (TextView) view.findViewById(R.id.progressTextView);		
	}
	
	public void refresh(Object item) {
		super.refresh(item);
		setLayoutParams(getImageDefWidth(), getImageDefHeight(), maskImageView, thumbnailImageView);

		VideoAttachment msgAttachment = (VideoAttachment)messageItem.getMessage().getAttachment();
		String thumbPath = msgAttachment.getThumbPath();
		if (TextUtils.isEmpty(thumbPath)) {
			setLayoutParams(getImageDefWidth(), getImageDefHeight(), maskImageView, thumbnailImageView);
			thumbnailImageView.setImageDrawable(null);
			
			if (messageItem.getMessage().getAttachStatus() == AttachStatusEnum.transferred)  {
				redownload();				
			}
		} else {
			loadThumbnailImage(thumbPath);	
		}
		
		thumbnailImageView.setOnClickListener(new View.OnClickListener() {	
			@Override
			public void onClick(View v) {
                //TODO fw WatchVideoActivity
				//WatchVideoActivity.start(context, messageItem.getMessage());
			}
		});				
		
		progressTextView.setText(StringUtil.getPercentString(((MsgListItem) item).progress));
	}

	private void loadThumbnailImage(String thumbPath) {
		int[] bound = ImageUtil.getBoundWithLength(getImageMaxEdge(), thumbPath, true);
		int width = bound[0];
		int height = bound[1];

		setLayoutParams(width, height, maskImageView, thumbnailImageView);
		thumbnailImageView.setImageBitmap(thumbnailImageView.loadAsPath(thumbPath, width, height,
				R.drawable.message_right_blue_bg,
				R.drawable.message_right_blue_cover_bg));
	}
}
